package ytdp.careagle.com.ytdp.activity;

import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.umeng.analytics.dplus.UMADplus;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;
import ytdp.careagle.com.ytdp.R;
import ytdp.careagle.com.ytdp.mvp.presenter.ForgetPasswordPresenter;
import ytdp.careagle.com.ytdp.mvp.view.IForgetPasswordView;
import ytdp.careagle.com.ytdp.myview.KeyboardChangeListener;
import ytdp.careagle.com.ytdp.utils.GetCheckCodeUtils;
import ytdp.careagle.com.ytdp.utils.camer_util.DisplayUtil;

public class ForgetPasswordActivity extends BaseActivity implements KeyboardChangeListener.KeyBoardListener, IForgetPasswordView {

    @Bind(R.id.iv_logo)
    ImageView ivLogo;
    @Bind(R.id.et_username)
    EditText etUsername;
    @Bind(R.id.iv_username_clear)
    ImageView ivUsernameClear;
    @Bind(R.id.line_bottom_username)
    View lineBottomUsername;
    @Bind(R.id.et_verification_code)
    EditText etVerificationCode;
    @Bind(R.id.line_bottom_verification_code)
    View lineBottomVerificationCode;
    @Bind(R.id.et_password)
    EditText etPassword;
    @Bind(R.id.iv_password_clear)
    ImageView ivPasswordClear;
    @Bind(R.id.line_bottom_password)
    View lineBottomPassword;
    @Bind(R.id.ll_login)
    LinearLayout llLogin;
    @Bind(R.id.btn_reset_password)
    Button btnResetPassword;
    @Bind(R.id.ll_gen)
    LinearLayout llGen;
    @Bind(R.id.scrollView)
    ScrollView scrollView;
    @Bind(R.id.tv_get_verification_code)
    TextView tvGetVerificationCode;
    private KeyboardChangeListener mKeyboardChangeListener;
    private boolean eyeSelected = false;
    private ForgetPasswordPresenter presenter;
    private Disposable checkCode;//倒计时
    private String username;

    @OnClick({R.id.iv_username_clear, R.id.iv_password_clear, R.id.btn_reset_password,
            R.id.tv_get_verification_code})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_username_clear:
                etUsername.setText("");
                break;
            case R.id.iv_password_clear://是否显示密码
                eyeSelected = !eyeSelected;
                if (eyeSelected) {
                    etPassword.setTransformationMethod(HideReturnsTransformationMethod
                            .getInstance());
                } else {
                    etPassword.setTransformationMethod(PasswordTransformationMethod
                            .getInstance());
                }
                etPassword.setSelection(etPassword.getText().length());//将光标移至文字末尾
                ivPasswordClear.setSelected(eyeSelected);
                break;
            case R.id.btn_reset_password:
                presenter.resetPassword(getWContext().get());
                break;
            case R.id.tv_get_verification_code:
                presenter.getVerificationCode(getWContext().get());
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ActivityCompat.finishAfterTransition(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_forget_password;
    }

    @Override
    public void initView() {
        UMADplus.track(this, "忘记密码页面", null);

        initEditTextListener();
        //不让他滑动
        scrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                return true;
            }
        });
        presenter = new ForgetPasswordPresenter(this);
        username = getIntent().getStringExtra("username");
        if (!TextUtils.isEmpty(username)) {
            etUsername.setText(username);
            etUsername.setFocusable(false);
        }

    }

    private void initEditTextListener() {
        //软键盘监听
        mKeyboardChangeListener = new KeyboardChangeListener(this);
        mKeyboardChangeListener.setKeyBoardListener(this);

        etPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onFocusChange(View view, boolean b) {
                int colorId = b ? R.color.guestPaint_color_wrong : R.color.line_color;
                lineBottomPassword.setBackground(getResources().getDrawable(colorId));
                ivPasswordClear.setVisibility(b ? View.VISIBLE : View.GONE);
            }
        });

        etUsername.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onFocusChange(View view, boolean b) {
                int colorId = b ? R.color.guestPaint_color_wrong : R.color.line_color;
                lineBottomUsername.setBackground(getResources().getDrawable(colorId));
                ivUsernameClear.setVisibility(b ? View.VISIBLE : View.GONE);
            }
        });
        etVerificationCode.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onFocusChange(View view, boolean b) {
                int colorId = b ? R.color.guestPaint_color_wrong : R.color.line_color;
                lineBottomVerificationCode.setBackground(getResources().getDrawable(colorId));
            }
        });


        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void afterTextChanged(Editable editable) {
                String password = etPassword.getText().toString();
                String username = etUsername.getText().toString();
                String verificationCode = etVerificationCode.getText().toString();
                if (!TextUtils.isEmpty(password) && !TextUtils.isEmpty(username) && !TextUtils.isEmpty(verificationCode)) {
                    btnResetPassword.setTextColor(getResources().getColor(R.color.white));
                    btnResetPassword.setBackground(getResources().getDrawable(R.drawable.shape_btn_org_selected));
                } else {
                    btnResetPassword.setTextColor(getResources().getColor(R.color.guestPaint_color_wrong_selected));
                    btnResetPassword.setBackground(getResources().getDrawable(R.drawable.shape_btn_pink_default));
                }
            }
        };
        etPassword.addTextChangedListener(textWatcher);
        etUsername.addTextChangedListener(textWatcher);
        etVerificationCode.addTextChangedListener(textWatcher);
    }

    @Override
    public void onKeyboardChange(boolean isShow, int keyboardHeight) {
        int y = DisplayUtil.dip2px(getApplicationContext(), 90);
        if (isShow) {
            llGen.animate().translationYBy(0).translationY(-y).setDuration(500);
            ivLogo.animate().scaleXBy(1).scaleX(0.5f).scaleYBy(1).scaleY(0.5f).setDuration(500);
        } else {
            llGen.animate().translationYBy(-y).translationY(0).setDuration(500);
            ivLogo.animate().scaleXBy(0.5f).scaleX(1).scaleYBy(0.5f).scaleY(1).setDuration(500);
        }
    }

    @Override
    public String getMobileNumber() {
        return etUsername.getText().toString();
    }

    @Override
    public void startTime() {
        checkCode = GetCheckCodeUtils.getCheckCode(60, tvGetVerificationCode, getResources().getColor(R.color.time_start), getResources().getColor(R.color.guestPaint_color_wrong));
    }

    @Override
    public String getNewPassword() {
        return etPassword.getText().toString();
    }

    @Override
    public String getCode() {
        return etVerificationCode.getText().toString();
    }

    @Override
    public void resetPasswordSuccess() {
        if (TextUtils.isEmpty(username)) {
            EventBus.getDefault().post(getMobileNumber());
            ActivityCompat.finishAfterTransition(ForgetPasswordActivity.this);
        } else {
            Intent intent = new Intent(this, LoginActivity.class);
            intent.putExtra("username", username);
            startActivity(intent);
            finish();
        }

    }

    @Override
    protected void onDestroy() {
        if (checkCode != null) {
            checkCode.dispose();
        }
        super.onDestroy();
    }
}
